﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DslamPort.Models;

namespace DslamPort.Controllers
{
    public class DslamPortftthController : Controller
    {
        
            DslamPortModel DslamPortModel = new DslamPortModel();
            DonviModel DonviModel = new DonviModel();
            public ActionResult Index()
            {
                string str = "";
                string dk = "";
                string sql = "";
                string status = "";
                int num;
                if (Session != null && Session["system_id"] != null && Session["quan_huyen_id"] != null && Session["todanpho_id"] != null)
                {
                    if (Session["select_option"].ToString() == "DslamPort.account_int")
                        dk = "a.account_int";
                    if (Session["select_option"].ToString() == "DslamPort.account_mytv")
                        dk = "a.account_mytv";
                    if (Session["select_status"].ToString() != "-1")
                        status = " and a.status=" + Session["select_status"].ToString();
                    if (int.TryParse(Session["quan_huyen_id"].ToString(), out num))
                        str = " and a.system_id='" + Session["system_id"] + "' and c.quan_huyen_id=" + Session["quan_huyen_id"] + status;

                    ViewBag.find_textbox = Session["timkiem"].ToString();
                    //---------------------------------------------------
                    string dk2 = "";
                    if (Request["select_option"] == "DslamPort.account_int")
                        dk = "a.account_int";
                    if (Request["select_option"] == "DslamPort.account_mytv")
                        dk = "a.account_mytv";

                    if (Request["select_status"] == "0")
                        dk2 = " and a.status=0";
                    if (Request["select_status"] == "1")
                        dk2 = " and a.status=1";
                    if (Request["select_status"] == "2")
                        dk2 = " and a.status=2";
                    if (ViewBag.find_textbox.Trim() == "")
                        sql = str + " and (" + dk + " like '%" + ViewBag.find_textbox.Trim() + "%' or " + dk + " is null)" + dk2;
                    else
                        sql = str + " and (" + dk + " like '%" + ViewBag.find_textbox.Trim() + "%' or upper(" + dk + ") like upper('%" + ViewBag.find_textbox.Trim() + "%'))" + dk2;

                    //-----------------------------------------------------
                    ViewBag.find_selectbox = Session["select_option"];
                    ViewBag.find_select_status = Session["select_status"].ToString();
                    ViewBag.selectedQuan_huyen = Session["quan_huyen_id"].ToString();
                    ViewBag.selectedSystem = Session["system_id"].ToString();
                    ViewBag.dslam_type = Session["dslam_type"];


                    ViewBag.selectedXaphuong = Session["xa_phuong_id"].ToString();
                    ViewBag.selectedTodanpho = Session["todanpho_id"].ToString();

                    //ViewBag.todanpho = new SelectList(DonviModel.getTodanpho(), "todanpho_id", "ten");
                    //ViewBag.khuvuc = new SelectList(DonviModel.getKhuvuc(), "quan_huyen_id", "ten_kv");

                    if (!int.TryParse(ViewBag.selectedQuan_huyen, out num))
                        ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                    else
                    {
                        if (!int.TryParse(ViewBag.selectedXaphuong, out num))
                        {
                            ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp");
                        }
                        else
                        {
                            if (!int.TryParse(ViewBag.selectedTodanpho, out num))
                            {
                                ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp");
                            }
                            else
                            {

                                ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp", ViewBag.selectedTodanpho);
                            }
                            ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp", ViewBag.selectedXaphuong);
                        }
                        ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", ViewBag.selectedQuan_huyen);
                        DslamModel DslamModel = new DslamModel();

                        string s = ViewBag.selectedQuan_huyen;
                        if (int.TryParse(ViewBag.selectedXaphuong, out num))
                            s += " and a.xa_phuong_id=" + ViewBag.selectedXaphuong;
                        if (int.TryParse(ViewBag.selectedTodanpho, out num))
                            s += " and a.todanpho_id=" + ViewBag.selectedTodanpho;
                        List<DslamModel> dslam = DslamModel.getDslamByDonvi(s,2);
                        ViewBag.dslam = new SelectList(dslam, "system_id", "name", ViewBag.selectedSystem);
                    }
                }
                else
                {
                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                }
                List<DslamPortModel> DslamPort = DslamPortModel.getDslamPort(sql);

                return View(DslamPort);
            }

            public ActionResult EditDslamPort(int id, string system_id, string quan_huyen_id, string select_status, string select_option, string timkiem)
            {
                //string str = " and a.system_id=" + system_id + " and c.quan_huyen_id=" + quan_huyen_id;
                DslamPortModel DslamPort = DslamPortModel.getDslamPort("", id.ToString()).Find(delegate(DslamPortModel s) { return Convert.ToInt32(s.id) == id; });
                if (quan_huyen_id.Length == 0)
                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                else
                {
                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", quan_huyen_id);
                    DslamModel DslamModel = new DslamModel();
                    List<DslamModel> dslam = DslamModel.getDslamByDonvi(quan_huyen_id,2);
                    ViewBag.dslam = new SelectList(dslam, "system_id", "name", system_id);

                    ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(quan_huyen_id), "xa_phuong_id", "ten_xp", DslamPort.quan_huyen_id);
                    ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(DslamPort.xa_phuong_id), "todanpho_id", "ten_tdp", DslamPort.todanpho_id);

                    ViewBag.congtrong = new SelectList(DslamPortModel.getDslamPort(" and a.system_id='" + system_id + "' and a.status=0 and cong!=" + DslamPort.cong + " and c.quan_huyen_id=" + quan_huyen_id), "cong", "cong");

                }

                ViewBag.system_id = system_id;
                ViewBag.quan_huyen_id = quan_huyen_id;
                ViewBag.select_status = select_status;
                ViewBag.select_option = select_option;
                ViewBag.timkiem = timkiem;
                return View(DslamPort);
            }
            public ActionResult RequestAction(string id, string type)
            {
                if (DslamPortModel.checkPort2(id) > 0 && type == "bh")
                {
                    TempData["error"] = "Chỉ có thể báo hỏng khi Port để trống";
                    return RedirectToAction("Index");
                }
                DslamPortModel DslamPort = DslamPortModel.getDslamPort("", id.ToString()).Find(delegate(DslamPortModel s) { return s.id == id; });
                DslamPortModel.updateRequestAction(DslamPort, id, type);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }

            [HttpPost]
            public ActionResult EditDslamPort(DslamPortModel DslamPort)
            {
                DslamPort.trang_thai_int = Request.Form["trang_thai_int"];
                DslamPort.hinh_thuc_int = Request.Form["hinh_thuc_int"];
                string a = Request.Form["hinh_thuc_int"];
                string sm_type = Request.Form["sm_type"];
                if (ModelState.IsValid)
                {
                    if (sm_type != "")
                    {
                        if (sm_type == "cn")
                        {
                            if (Request.Form["congtrong"] == Request.Form["cong_old"].Trim() && DslamPort.checkPort(DslamPort) > 0)
                            {
                                string quan_huyen_id = Request.Form["quan_huyen_id"];
                                TempData["error"] = "Port đã được sử dụng hoặc không tồn tại!";
                                if (quan_huyen_id.Length == 0)
                                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                                else
                                {
                                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", quan_huyen_id);
                                    DslamModel DslamModel = new DslamModel();
                                    List<DslamModel> dslam = DslamModel.getDslamByDonvi(quan_huyen_id,2);
                                    ViewBag.dslam = new SelectList(dslam, "system_id", "name", DslamPort.system_id);
                                }
                                return View(DslamPort);
                            }
                        }

                        if (DslamPort.account_int != "" || DslamPort.account_mytv != "")
                            DslamPort.status = "1";
                        var checkbox = Request.Form.Get("dc_checkbox");
                        if (checkbox != null)
                        {
                            if (DslamPort.system_id != Request.Form["system_old"].Trim() || Request.Form["congtrong"] != Request.Form["cong_old"].Trim())
                            {
                                sm_type = "dc";
                                DslamPort.cong = Request.Form["congtrong"];
                                if (DslamPort.checkPort(DslamPort, "dk2") == 0)
                                {
                                    string quan_huyen_id = Request.Form["quan_huyen_id"];
                                    TempData["error"] = "Port đã được sử dụng hoặc không tồn tại!";
                                    ViewBag.quan_huyen_id = quan_huyen_id;
                                    ViewBag.system_id = DslamPort.system_id;
                                    ViewBag.system_id = DslamPort.system_id;
                                    ViewBag.select_status = Request.Form["select_status"];
                                    ViewBag.select_option = Request.Form["select_option"];
                                    ViewBag.timkiem = Request.Form["timkiem"];
                                    if (quan_huyen_id.Length == 0)
                                        ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                                    else
                                    {
                                        ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", quan_huyen_id);
                                        DslamModel DslamModel = new DslamModel();
                                        List<DslamModel> dslam = DslamModel.getDslamByDonvi(quan_huyen_id,2);
                                        ViewBag.dslam = new SelectList(dslam, "system_id", "name", DslamPort.system_id);

                                        ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(quan_huyen_id), "quan_huyen_id", "ten_kv", DslamPort.quan_huyen_id);
                                        ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(DslamPort.quan_huyen_id), "todanpho_id", "ten", DslamPort.todanpho_id);
                                    }
                                    return View(DslamPort);
                                }

                            }
                        }
                        DslamPortModel.updateDslam(DslamPort, sm_type);
                        TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                        //return View(DslamPort);
                        return RedirectToAction("Index");
                    }

                }
                TempData["error"] = "Bạn đã nhập sai thông tin!";
                //ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                //Dslam_TypeModel Dslam_TypeModel = new Dslam_TypeModel();
                //ViewBag.dslam_type = new SelectList(Dslam_TypeModel.getDslam_Type(), "id", "name");
                return View(DslamPort);
            }

            public ActionResult FindDslamPort()
            {
                return RedirectToAction("Index");
            }
            [HttpPost]
            public ActionResult FindDslamPort(string timkiem)
            {
                //string quan_huyen_id = "";
                string system_id = "";

                string quan_huyen_id = Request.Form["quan_huyen_id"];
                ViewBag.selectedQuan_huyen = quan_huyen_id;
                string xa_phuong_id = Request.Form["xa_phuong_id"];
                ViewBag.selectedXaphuong = xa_phuong_id;
                string todanpho_id = Request.Form["todanpho_id"];
                ViewBag.selectedTodanpho = todanpho_id;

                int num;
                string s = "";
                ViewBag.selectedQuan_huyen = "";
                ViewBag.selectedSystem = "";
                if (int.TryParse(Request.Form["quan_huyen_id"], out num))
                {
                    //quan_huyen_id = Request.Form["quan_huyen_id"];
                    s += " b.quan_huyen_id=" + quan_huyen_id + " and ";
                    ViewBag.selectedQuan_huyen = Request.Form["quan_huyen_id"];
                }
                if (Request.Form["system_id"] != "")
                {
                    system_id = Request.Form["system_id"];
                    s += " a.system_id='" + system_id + "' and ";
                    ViewBag.selectedSystem = Request.Form["system_id"];
                }
                if (Request.Form["xa_phuong_id"] != "")
                    s += " b.xa_phuong_id=" + xa_phuong_id + " and ";

                if (Request.Form["todanpho_id"] != "")
                    s += " b.todanpho_id=" + todanpho_id + " and ";
                //if (int.TryParse(Request.Form["quan_huyen_id"], out num) && Request.Form["system_id"] != "")
                //    s = " c.quan_huyen_id=" + quan_huyen_id + " AND a.system_id='" + system_id + "' AND ";
                string sql;
                string dk = "";
                string dk2 = "";
                if (Request["select_option"] == "DslamPort.account_int")
                    dk = "a.account_int";
                if (Request["select_option"] == "DslamPort.account_mytv")
                    dk = "a.account_mytv";

                if (Request["select_status"] == "0")
                    dk2 = " and a.status=0";
                if (Request["select_status"] == "1")
                    dk2 = " and a.status=1";
                if (Request["select_status"] == "2")
                    dk2 = " and a.status=2";
                ViewBag.find_select_status = Request["select_status"];

                if (timkiem.Trim() == "")
                    sql = "and " + s + "(" + dk + " like '%" + timkiem.Trim() + "%' or " + dk + " is null)" + dk2;
                else
                    sql = "and " + s + "(" + dk + " like '%" + timkiem.Trim() + "%' or upper(" + dk + ") like upper('%" + timkiem.Trim() + "%'))" + dk2;

                ViewBag.find_textbox = timkiem;
                ViewBag.find_selectbox = Request["select_option"];
                List<DslamPortModel> DslamPort = new List<DslamPortModel>();
                if (timkiem.Trim().Length >= 2)
                {
                    DslamPort = DslamPortModel.getDslamPort(sql, "", "1");
                }
                else
                {
                    DslamPort = DslamPortModel.getDslamPort(sql);
                }
                ViewBag.dslam_type = 1;
                try
                {
                    if (DslamPort.First().rackid == "")
                        ViewBag.dslam_type = 0;
                }
                catch { }

                if (ViewBag.selectedQuan_huyen.Length == 0)
                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh");
                else
                {
                    if (!int.TryParse(ViewBag.selectedXaphuong, out num))
                    {
                        ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp");
                    }
                    else
                    {
                        if (!int.TryParse(ViewBag.selectedTodanpho, out num))
                        {
                            ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp");
                        }
                        else
                        {
                            ViewBag.todanpho = new SelectList(DonviModel.getTodanphoByKhuvuc(ViewBag.selectedXaphuong), "todanpho_id", "ten_tdp", ViewBag.selectedTodanpho);
                        }
                        ViewBag.khuvuc = new SelectList(DonviModel.getKhuvucByDonvi(ViewBag.selectedQuan_huyen), "xa_phuong_id", "ten_xp", ViewBag.selectedXaphuong);
                    }
                    ViewBag.donvi = new SelectList(DonviModel.getDonvi(), "quan_huyen_id", "ten_qh", ViewBag.selectedQuan_huyen);
                    DslamModel DslamModel = new DslamModel();

                    string s1 = ViewBag.selectedQuan_huyen;
                    //if (ViewBag.selectedQuan_huyen.Length != 0)
                    if (int.TryParse(ViewBag.selectedQuan_huyen, out num))
                        s1 += " and a.quan_huyen_id=" + ViewBag.selectedQuan_huyen;
                    //if (ViewBag.selectedTodanpho.Length != 0)
                    if (int.TryParse(ViewBag.selectedTodanpho, out num))
                        s1 += " and a.todanpho_id=" + ViewBag.selectedTodanpho;
                    List<DslamModel> dslam = DslamModel.getDslamByDonvi(s1,2);
                    ViewBag.dslam = new SelectList(dslam, "system_id", "name", ViewBag.selectedSystem);
                  
                   
                    try
                    {
                        DslamModel ds = DslamModel.getDslam(2, "").Find(delegate(DslamModel u) { return u.system_id == system_id; });
                        ViewBag.ip = "IP: " + ds.ip;
                        ViewBag.system_id = " System_id: " + ds.system_id;
                    }
                    catch (System.Exception ex)
                    {

                    }
                }

                //ViewBag.todanpho = new SelectList(DonviModel.getTodanpho(), "todanpho_id", "ten");
                //ViewBag.khuvuc = new SelectList(DonviModel.getKhuvuc(), "quan_huyen_id", "ten_kv");

                Session["dslam_type"] = ViewBag.dslam_type;
                Session["system_id"] = system_id;
                Session["todanpho_id"] = todanpho_id;
                Session["quan_huyen_id"] = quan_huyen_id;
                Session["xa_phuong_id"] = xa_phuong_id;
                Session["select_status"] = Request["select_status"];
                Session["select_option"] = Request["select_option"];
                Session["timkiem"] = timkiem;
                return View("Index", DslamPort);
            }
            [HttpPost]
            public JsonResult getKhuvucJson(string quan_huyen_id, string Order)
            {
                return Json(DslamPortModel.getKhuvucJson(quan_huyen_id, Order));
            }
            [HttpPost]
            public JsonResult getTodanphoJson(string xa_phuong_id, string Order)
            {
                return Json(DslamPortModel.getTodanphoJson(xa_phuong_id, Order));
            }
            [HttpPost]
            public JsonResult getDslamJson(string quan_huyen_id, string Order)
            {
                return Json(DslamPortModel.getDslamJson(2,quan_huyen_id, Order));
            }
            [HttpPost]
            public JsonResult getDslamByKhuvucJson(string xa_phuong_id, string Order)
            {
                return Json(DslamPortModel.getDslamByKhuvucJson(2,xa_phuong_id, Order));
            }
            [HttpPost]
            public JsonResult getDslamByTodanphoJson(string todanpho_id, string Order)
            {
                return Json(DslamPortModel.getDslamByTodanphoJson(2,todanpho_id, Order));
            }
            [HttpPost]
            public JsonResult getCongtrongJson(string system_id, string system_id_old, string cong, string Order)
            {
                return Json(DslamPortModel.getCongtrongJson(system_id, system_id_old, cong, Order));
            }
            //public ActionResult DeleteDslamPort(string id)
            //{
            //    DslamPortModel.DeleteDslamPort(id);
            //    TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            //    return RedirectToAction("Index");
            //}
            public ActionResult Xem()
            {
                ViewBag.donvi = new SelectList(DonviModel.getDonvi("str"), "quan_huyen_id", "ten_qh");

                return View();
            }
        

    }
}
